$(function(){
    //获取关键字
    var keyword = decodeURIComponent(tools.queryString().keyword);
    if(keyword === "undefined") {
        $(".searchInput").html("请输入你要搜索的宝贝");
    }else {
        $(".searchInput").html(keyword);
    }
    var flag = false;
    var pageNum = 1;
    function loadSearchResult() {
        if (flag) return false;
        $.ajax({
            // url: domain.test + "/getkeywordresult",
            url: domain.online + "/api/product/list",
            type: 'get',
            data: {
                keyword:encodeURIComponent(keyword),
                pageNum:pageNum
            },
            dataType: 'json',
            beforeSend: function () {
                flag = true;
            },
            success: function (response) {
                // var resDetail = response.data.data;
                var resDetail = response.data;
                template.helper("total",function(){
                    return resDetail.total;
                });
                if (pageNum == 1) {
                    $('.itemBox').html(template('itemBox', { productList: resDetail.plist }));
                } else {
                    $('.itemBox').append(template('itemBox', { productList: resDetail.plist }));
                }
                pullmaster.pullDownSuccess();       //刷新成功
                if (resDetail.plist.length < 10 || resDetail.plist.length === 0) {
                    pullmaster.pullUpDone();        //到底啦
                    $(".bb_pull-up").css({"display":"none"});
                    if(resDetail.total!==0) {
                        $(".end").css({"display":"block"});
                    }
                }
                ++pageNum;
                flag = false;
            },
            error: function () {
                pullmaster.pullDownFailed();        //刷新失败
                pullmaster.pullUpFailed();          //数据加载失败
            }
        })
    }
    var pullmaster = new Pull($('.itemBox'), {
        onPullUp: function () {
            loadSearchResult();
        },
        onPullDown: function() {
            pageNum = 1;
            loadSearchResult();
        }
    });
    //历史回退
    $(".arrowIcon").on("click",function() {
        history.back();
    });
    $(".searchInput").on("click",function() {
        history.back();
    });
    //查看商品详情
    $(".itemBox").on('click',".productItem",function() {
        var pId = $(this).attr("data-id");
        location.href = "../classify/product.html?id=" + pId;
    })
});